<?php

include_once ('bn_path.php');

class bn_DropDownList
{
    public $text='name';
    public $value='id';
    public $selected = '';
    public $name = 'dropdownlist1';
    public $items;
    public $cssclass = '';
    protected static $instance;

    public function __construct($fieldval = '', $fieldtxt = '')
    {
        $this->text = $fieldtxt;
        $this->value = $fieldval;
        $this->items = new ArrayObject();
    }

    public static function getInstance()
    {
        if (!isset(self::$instance))
        {
            self::$instance = new self();
        }
        return self::$instance;
    }

    private function create_list($value, $text)
    {
        if ($this->selected == $value || $this->selected == $text)
            return "\n<option value='$value' selected>$text</option>";
        else
            return "\n<option value='$value'>$text</option>";
    }

    private function create_items()
    {
        $out = '';
        foreach ($this->items as $item)
        {
            if (is_object($item))
            {
                if ($item->is_selected)
                    $this->selected = $item->value;
                $out .= self::create_list($item->value, $item->text);
            }
        }
        return $out;
    }

    public function show($param = array(),$echo=false)
    {
        if ($param != null || $this->items != null)
        {
            $row = $param[0];
            if (is_object($row))
                $row = (array )$row;
            if (is_array($row))
            {
                $key = array_keys($row);
                if ($this->value == '')
                    $this->value = $key[0];
                if ($this->text == '')
                    $this->text = $key[1];
            }
            if ($this->cssclass != '')
                $this->cssclass = " class='$this->cssclass'";
            $dropdownlist = "\n<select name='$this->name' id='$this->name'$this->cssclass>";
            $count = count($param);
            foreach ($param as $row)
            {
                if (is_object($row))
                    $row = (array )$row;
                if (count($row) == 1)
                    $dropdownlist .= self::create_list($row, $row);
                else
                {
                    if ($this->text != '' && $this->value != '')
                        $dropdownlist .= self::create_list($row[$this->value], $row[$this->text]);
                    else
                        $dropdownlist .= self::create_list($row[0], $row[1]);
                }
            }
            $dropdownlist .= self::create_items();
            $dropdownlist .= "\n</select>";
            if($echo)
            echo $dropdownlist;
            else
            return $dropdownlist;
        }
    }

    public function __destruct()
    {
        unset($this->items);
    }
}

?>